<h1>Award Scholarships</h1>
<%@ page import="java.io.File"%>
<script type="text/javascript">
	function applyClicked(scholarship_id) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "apply.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "scholarship_id");
		data.setAttribute('value', scholarship_id);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
	function awardClicked(scholarship_id) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "award.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "scholarship_id");
		data.setAttribute('value', scholarship_id);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
</script>
<form action="dashboard.jsp">
	<table class="center">
		<tr>
			<td><input type="text" name="search" size="50"> <input type="submit" value="Search"></td>
		</tr>
	</table>
</form>
<form name="apply" action="apply.jsp">
	<table>
		<tr>
			<td>
				<%
					session.setAttribute("file", "award_scholarship.jsp");
					// Provide default values for variables.
					if (session.getAttribute("errors") == null) {
						session.setAttribute("errors", "");
					}
					if (session.getAttribute("search") == null) {
						session.setAttribute("search", "");
					}

					// Retrieve submitted values.
					String search = request.getParameter("search");
					if (search == null) {
						search = (String) session.getAttribute("search");
					}
				%>
				<h1>
					Search Results:
					<%=search%></h1> <%
 	// Display list of scholarships.
 	java.sql.Connection conn = null;
 	try {
 		String myJDBCDriver = (String) session.getAttribute("myJDBCDriver");
 		String myURL = (String) session.getAttribute("myURL");
 		String myUserID = (String) session.getAttribute("myUserID");
 		String myPassword = (String) session.getAttribute("myPassword");

 		// Connect to the database.
 		Class.forName(myJDBCDriver).newInstance();
 		java.util.Properties sysprops = System.getProperties();
 		sysprops.put("user", myUserID);
 		sysprops.put("password", myPassword);
 		conn = java.sql.DriverManager.getConnection(myURL, sysprops);

 		java.sql.Statement statement = conn.createStatement();
 		java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM scholarships WHERE name LIKE '%" + search + "%' OR sponsors LIKE '%"
 				+ search + "%' OR major LIKE '%" + search + "%' OR description LIKE '%" + search + "%'");
 		java.sql.ResultSetMetaData columns = rs.getMetaData();
 		while (rs.next()) {
 			if (rs.getInt("finished") == 1) {
 				out.println("<table id=\"scholarship\">");
 				for (int i = 2; i <= columns.getColumnCount() - 2; i++) {
 					out.println("<tr>");
 					out.println("<th>" + columns.getColumnName(i) + ":</th>");
 					out.println("<td>" + rs.getString(i) + "</td>");
 					out.println("</tr>");
 				}
 				String docPath = application.getRealPath("scholarship_documents") + "\\" + rs.getString("id") + ".doc";
 				File file = new File(docPath);
 				if (file.exists()) {
 					out.println("<tr><th>extra_info</th><td><a href=\"" + "scholarship_documents/" + file.getName()
 							+ "\">Download Extra Information</a></td></tr>");
 				}
 				if (((String) session.getAttribute("type")).equalsIgnoreCase("student")) {
 					out.println("<tr><td></td><td><a href=\"Javascript:void();\" class=\"right red_button\" onclick=\"applyClicked("
 							+ rs.getString("id") + ")\">Apply</a></td></tr>");
 				}
 				if (((String) session.getAttribute("type")).equalsIgnoreCase("reviewer")) {
 					out.println("<tr><td></td><td><a href=\"Javascript:void();\" class=\"right red_button\" onclick=\"reviewClicked("
 							+ rs.getString("id") + ")\">Review Applications</a></td></tr>");
 				}
 				if (((String) session.getAttribute("type")).equalsIgnoreCase("dean")) {
 					out.println("<tr><td></td><td><a href=\"Javascript:void();\" class=\"right red_button\" onclick=\"awardClicked("
 							+ rs.getString("id") + ")\">Award Applicant</a></td></tr>");
 				}
 				out.println("</table>");
 				out.println("<br />");
 			}
 		}
 	} catch (Exception e) {
 		e.printStackTrace();
 		out.print(e.toString());
 	} finally {
 		try {
 			conn.close();
 		} catch (Exception ee) {
 		}
 	}

 	// Clear variables after reading them.
 	session.setAttribute("errors", null);
 	session.setAttribute("search", null);
 %>
			</td>
		</tr>
	</table>
</form>